<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="交易时间" prop="dealDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.dealDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="交易时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <template>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="收入明细" name="first">
          <el-table v-loading="loading" :data="dayList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />

            <el-table-column label="交易时间" align="center" prop="dealDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.dealDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="总计" align="center" prop="total" />
            <el-table-column label="押金" align="center" prop="deposit" />
            <el-table-column label="租金" align="center" prop="rent" />
            <el-table-column label="电费" align="center" prop="electricityFee" />
            <el-table-column label="水费" align="center" prop="waterFee" />
            <el-table-column label="宽带费" align="center" prop="netFee" />
            <el-table-column label="燃气费" align="center" prop="gasFee" />
            <el-table-column label="有线电视费" align="center" prop="cableFee" />
            <el-table-column label="其他" align="center" prop="otherFee" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:day:edit']">查看</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-tab-pane>
        <el-tab-pane label="支出明细" name="second">
          <el-table v-loading="loading" :data="dayList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />

            <el-table-column label="交易时间" align="center" prop="dealDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.dealDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="总计" align="center" prop="total" />
            <el-table-column label="押金" align="center" prop="deposit" />
            <el-table-column label="租金" align="center" prop="rent" />
            <el-table-column label="电费" align="center" prop="electricityFee" />
            <el-table-column label="水费" align="center" prop="waterFee" />
            <el-table-column label="宽带费" align="center" prop="netFee" />
            <el-table-column label="燃气费" align="center" prop="gasFee" />
            <el-table-column label="有线电视费" align="center" prop="cableFee" />
            <el-table-column label="其他" align="center" prop="otherFee" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:day:edit']">查看</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-tab-pane>
      </el-tabs>
    </template>


    <!-- 添加或修改海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
<span>交易时间:{{this.planParams.realCollectionDate}}   收入金额统计:{{this.sum}}</span>
      <template>
        <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="id"
            label="序号"
            width="180">
          </el-table-column>
          <el-table-column
            prop="dealDate"
            label="交易时间"
          >
            <span>{{this.planParams.realCollectionDate}}</span>
          </el-table-column>
          <el-table-column
            prop="roomName"
            label="房源名称"
            width="180">
          </el-table-column>
          <el-table-column
            prop="tenantName"
            label="租客姓名">
          </el-table-column>
          <el-table-column
            prop="receiptType"
            label="费用类型">
            <template slot-scope="scope">
              <span v-if="scope.row.receiptType==1">
                房租收款
              </span>
              <span v-if="scope.row.receiptType==2">
                租务收款
              </span>
              <span v-if="scope.row.receiptType==3">
                解约结算收款
              </span>
              <span v-if="scope.row.receiptType==4">
                续约结算收款
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="skStatus"
            label="收支状态">
            <template slot-scope="scope">
              <span v-if="scope.row.skStatus==1">
                未收款
              </span>
              <span v-if="scope.row.skStatus==2">
                已收款
              </span>
              <span v-if="scope.row.skStatus==3">
                已终止
              </span>
              <span v-if="scope.row.skStatus==4">
                已冻结
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="realPaymentAmount"
            label="金额(元)">
          </el-table-column>
          <el-table-column
            prop="payMethod"
            label="支付方式">
            <template slot-scope="scope">
              <span v-if="scope.row.payMethod==1">
               快捷同
              </span>
              <span v-if="scope.row.payMethod==2">
                支付宝
              </span>
              <span v-if="scope.row.payMethod==3">
                微信
              </span>
              <span v-if="scope.row.payMethod==4">
                现金
              </span>
              <span v-if="scope.row.payMethod==5">
                银行卡转账
              </span>
              <span v-if="scope.row.payMethod==6">
                POS刷卡
              </span>
              <span v-if="scope.row.payMethod==7">
                其他
              </span>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="pre">上一天</el-button>
        <el-button type="primary" @click="nexte">下一天</el-button>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDay, getDay, delDay, addDay, updateDay } from "@/api/financial/day";
import { listPlan, getPlan, delPlan, addPlan, updatePlan } from "@/api/financial/plan";
import {formatDate} from "@/utils";

export default {
  name: "Day",
  components: {
  },
  data() {
    return {
      tableData: [],
      activeName: 'second',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)表格数据
      dayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      sum:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        businessName: null,
        onlineFlg: null,
        dealDate: null,
        operationType: null,
        total: null,
        deposit: null,
        rent: null,
        electricityFee: null,
        waterFee: null,
        netFee: null,
        gasFee: null,
        cableFee: null,
        czFee: null,
        otherFee: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      planParams: {
        pageNum: 1,
        pageSize: 10000,
        receiptOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        receiptType: 1,
        skStatus: null,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withReceiptPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        collectionDate: null,
        realCollectionDate: null,
        receiptNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.queryParams.operationType=2;
    this.getList();
  },
  methods: {
    handleClick(tab, event) {
      if (tab.index==0) {
        this.queryParams.operationType=1;
        this.getList();
      }
      if (tab.index==1) {
        this.queryParams.operationType=2;
        this.getList();
      }

    },
    /** 查询海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)列表 */
    getList() {
      this.loading = true;
      listDay(this.queryParams).then(response => {
        this.dayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    pre(){

      const  dates=new Date(this.planParams.realCollectionDate);
      dates.setTime(dates.getTime()- 3600 * 1000 * 24);
      let  time=dates.toLocaleDateString();
      this.planParams.realCollectionDate=time;
      this.planList();
    },
    nexte(){
      const  dates=new Date(this.planParams.realCollectionDate);
      dates.setTime(dates.getTime()+ 3600 * 1000 * 24);
      let  time1=dates.toLocaleDateString();
      this.planParams.realCollectionDate=time1;
      this.planList();
    },planList(){
      this.loading = true;
      this.sum=0.000;

      listPlan(this.planParams).then(response => {
        this.tableData = response.rows;
        this.tableData.forEach(resp=>{
          this.sum+=resp.realPaymentAmount;
        })
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        businessName: null,
        onlineFlg: null,
        dealDate: null,
        operationType: null,
        total: null,
        deposit: null,
        rent: null,
        electricityFee: null,
        waterFee: null,
        netFee: null,
        gasFee: null,
        cableFee: null,
        czFee: null,
        otherFee: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.planParams.realCollectionDate=row.dealDate;
      console.log(this.planParams.realCollectionDate)
      const date=new Date(this.planParams.realCollectionDate)
      console.log(date.getTime());
    this.planList();
      this.open=true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDay(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDay(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDay(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/day/export', {
        ...this.queryParams
      }, `system_day.xlsx`)
    }
  }
};
</script>
